package org.datascooter.meta;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Container for table metadata, received from JDBC driver
 * 
 * @author nemo
 * 
 */
public class MetaTable implements Serializable {

	private static final long serialVersionUID = 3757928966633175906L;
	private String sheme;
	private String name;
	private List<MetaColumn> columns = new ArrayList<MetaColumn>();
	private List<Object[]> data = new ArrayList<Object[]>();
	private List<MetaLink> importedKeys = new ArrayList<MetaLink>();
	private List<MetaLink> exportedKeys = new ArrayList<MetaLink>();
	private List<MetaLink> selfKeys = new ArrayList<MetaLink>();
	private List<MetaPrimaryKey> primaryKeys = new ArrayList<MetaPrimaryKey>();
	private List<MetaIndex> indexList = new ArrayList<MetaIndex>();

	public MetaTable(String sheme, String name, List<MetaColumn> columns) {
		this.sheme = sheme;
		this.name = name;
		this.columns = columns;
	}

	public MetaTable(String sheme, String name, List<MetaColumn> columns, List<MetaLink> importedKeys, List<MetaLink> exportedKeys, List<MetaPrimaryKey> primaryKeys, List<MetaIndex> indexList) {
		this.sheme = sheme;
		this.name = name;
		this.columns = columns;
		if (columns != null) {
			this.columns = columns;
			Collections.sort(this.columns, new Comparator<MetaColumn>() {

				@Override
				public int compare(MetaColumn o1, MetaColumn o2) {
					return o1.ORDINAL_POSITION - o2.ORDINAL_POSITION;
				}
			});
		}
		if (importedKeys != null) {
			this.importedKeys = importedKeys;
			Collections.sort(this.importedKeys, new Comparator<MetaLink>() {

				@Override
				public int compare(MetaLink o1, MetaLink o2) {
					return o1.KEY_SEQ - o2.KEY_SEQ;
				}
			});
		}
		if (exportedKeys != null) {
			this.exportedKeys = exportedKeys;
			Collections.sort(this.exportedKeys, new Comparator<MetaLink>() {

				@Override
				public int compare(MetaLink o1, MetaLink o2) {
					return o1.KEY_SEQ - o2.KEY_SEQ;
				}
			});
		}
		if (primaryKeys != null) {
			this.primaryKeys = primaryKeys;
			Collections.sort(this.primaryKeys, new Comparator<MetaPrimaryKey>() {

				@Override
				public int compare(MetaPrimaryKey o1, MetaPrimaryKey o2) {
					return o1.KEY_SEQ - o2.KEY_SEQ;
				}
			});
		}
		if (indexList != null) {
			this.indexList = indexList;
			Collections.sort(this.indexList, new Comparator<MetaIndex>() {

				@Override
				public int compare(MetaIndex o1, MetaIndex o2) {
					return o1.ORDINAL_POSITION - o2.ORDINAL_POSITION;
				}
			});
		}
	}

	public List<Object[]> getData() {
		return data;
	}

	public void setData(List<Object[]> data) {
		this.data = data;
	}

	public void addRow(Object[] row) {
		data.add(row);
	}

	public List<MetaLink> getImportedKeys() {
		return importedKeys;
	}

	public List<MetaLink> getExportedKeys() {
		return exportedKeys;
	}

	public List<MetaLink> getSelfKeys() {
		return selfKeys;
	}

	public void addSelfKey(MetaLink selfKey) {
		this.selfKeys.add(selfKey);
	}

	public List<MetaPrimaryKey> getPrimaryKeys() {
		return primaryKeys;
	}

	public List<MetaIndex> getIndexList() {
		return indexList;
	}

	public String getSheme() {
		return sheme;
	}

	public void setSheme(String sheme) {
		this.sheme = sheme;
	}

	public String getName() {
		return name;
	}

	public List<MetaColumn> getColumns() {
		return columns;
	}
}
